package com.situ.edu.controller;

import com.situ.edu.pojo.entity.Course;
import com.situ.edu.pojo.entity.Elective;
import com.situ.edu.pojo.entity.Student;
import com.situ.edu.pojo.entity.User;
import com.situ.edu.pojo.query.CourseQuery;
import com.situ.edu.service.IStudentCouService;
import com.situ.edu.service.impl.StudentCouServiceImpl;
import com.situ.edu.util.MyBeanUtils;
import com.situ.edu.util.PageInfo;
import com.situ.edu.util.Result;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@WebServlet("/studentCou")
public class StudentCouController extends BaseController{
    private IStudentCouService studentCouService = new StudentCouServiceImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");
        switch (method){
            case "selectByPage":
                selectByPage(req,resp);
                break;
            case "selectAll":
                selectAll(req,resp);
                break;
            case "add":
                add(req,resp);
                break;
            case "selectById": //拿学生id
                selectById(req,resp);
                break;
            case "updateStu"://学生基本信息
                updateStu(req,resp);
                break;
            case "addElective": //学生选修课--update
                addElective(req,resp);
                break;
        }
    }


    //
    private void selectById(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("StudentCouController.selectById");
        //获得userId
        String id = req.getParameter("id");
        //根据userId获得studentId
        Student student = studentCouService.selectById(Integer.parseInt(id));

        toJSON(resp,Result.ok(student));
    }

    private void updateStu(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("StudentCouController.updateStu");
        Student student  = MyBeanUtils.copyToBean(req, Student.class);
        studentCouService.updateStu(student);
        toJSON(resp,Result.ok("更新成功"));
    }

    private void addElective(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("StudentCouController.addElective");
        String id = req.getParameter("id");
        String courseId = req.getParameter("courseId");
        studentCouService.addElective(Integer.parseInt(id),Integer.parseInt(courseId));
        toJSON(resp,Result.ok("选课成功"));
    }

    private void selectAll(HttpServletRequest req, HttpServletResponse resp) {
        List<Student> list = studentCouService.selectAll();
        toJSON(resp, Result.ok(list));
    }

    private void add(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("StudentCouController.add");
        Elective elective = MyBeanUtils.copyToBean(req,Elective.class);
        studentCouService.add(elective);
        toJSON(resp, Result.ok("选课成功"));

    }

    private void selectByPage(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("StudentCouController.selectByPage");
        CourseQuery courseQuery = MyBeanUtils.copyToBean(req,CourseQuery.class);
        PageInfo pageInfo = studentCouService.selectByPage(courseQuery);
        toJSON(resp,pageInfo);
    }
}
